<template>
  <div class="page">
    <div class="left">
      <ContentWrap>
        <!-- 搜索工作栏 -->
        <el-form
          class="-mb-15px"
          :model="queryParams"
          ref="queryFormRef"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="资源名称" prop="nodeName">
            <el-input
              v-model="queryParams.nodeName"
              placeholder="请输入资源名称"
              clearable
              @keyup.enter="handleQuery"
              class="!w-240px"
            />
          </el-form-item>
          <el-form-item>
            <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
            <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
          </el-form-item>
        </el-form>
      </ContentWrap>

      <!-- 列表 -->
      <ContentWrap>
        <el-tree
          class="filter-tree"
          :loading="loading"
          :data="list"
          :props="defaultProps"
          default-expand-all @node-click="clickNode"
          ref="tree"/>
      </ContentWrap>
    </div>
    <div class="right">
      <ContentWrap>
        <div v-html="strhtml" style="width: 100%;height: 100%"></div>
      </ContentWrap>
    </div>
  </div>

</template>

<script setup lang="ts">
import { handleTree } from '@/utils/tree'
import download from '@/utils/download'
import { TreeApi, Tree } from '@/api/knowledge/tree'
import {BaseTreeDetailApi} from "@/api/knowledge/item";


/** 知识库结构树 列表 */
defineOptions({ name: 'KnowledgeDisplayTree' })
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<Tree[]>([]) // 列表的数据
const queryParams = reactive({
  createTime: [],
  parentId: undefined,
  nodeName: undefined
})

const strhtml = ref('')

const defaultProps = ref({
  children: 'children',
  label: 'nodeName'
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await TreeApi.getTreeList(queryParams)
    list.value = handleTree(data, 'id', 'parentId')
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}


const clickNode = async (data, node, treeDom) => {
  console.debug(data)
  const itemData = await BaseTreeDetailApi.getBaseTreeDetail(data.id)
  if(null !== itemData){
    strhtml.value = itemData.html
  }
}
/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
<style scoped lang="scss">
.page {
  height: 100%;
  display: grid;
  grid-template-columns: 1fr 4fr;
  grid-gap: 20px;
}
.left {
  display: flex;
  flex-direction: column;
}
.right {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
</style>
